package com.cssw.fyzb.busi.api.exchange;

import com.cssw.fyzb.busi.api.user.GetUserInfo;
import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.core.model.vo.exchang.YExchangeFDefVo;
import com.cssw.fyzb.core.model.vo.msg.MsgVo;
import com.cssw.fyzb.core.model.vo.task.TaskAndInstVo;
import com.cssw.fyzb.core.model.vo.user.PublicUserInfoVo;
import com.cssw.fyzb.busi.service.exchange.YExchangeFDefService;
import com.cssw.fyzb.core.model.dodb.YExchangeFDef;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Function(description = "音值兑换福币", parameters = {
}, result = @Result(
        listProperties = {
                @ListProperty(name = "list", properties = {
                        @Property(name = "yf_id", type = Type.Integer, description = "主键标识"),
                        @Property(name = "y_coin", type = Type.String, description = "音值"),
                        @Property(name = "f_coin", type = Type.String, description = "福币"),
                })
        })
)

@Component
public class YExchangeFDefList implements Action {
    @Autowired
    YExchangeFDefService yExchangeFDefService;
    private static final Logger logger = LoggerFactory.getLogger(GetUserInfo.class);

    @Override
    public Response execute(Request request) throws Exception {
        try {
            Page<YExchangeFDef> page= yExchangeFDefService.list();

            List<YExchangeFDefVo> list = ((Converter<List<YExchangeFDef>, List<YExchangeFDefVo>>) yExchangeF -> {
                List<YExchangeFDefVo> vos = new ArrayList<>();
                yExchangeF.forEach((v) -> {
                    YExchangeFDefVo vo = new YExchangeFDefVo();
                    vo.setYfId(v.getYfId());
                    vo.setFeCoin(v.getFCoin().setScale(2));
                    vo.setYeCoin(v.getYCoin().setScale(2));
                    vos.add(vo);
                });
                return vos;
            }).convert(page.getList());
            return Response.success().put("list", list).put("total", page.getTotalPage()).put("count", page.getTotalRow());
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }
}
